#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion


using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace IPSim.GUI.Devices
{
    /// <author>Edward Smit</author>
    public class LineDevice: BaseDevice, IControllable
    {
        #region Members

        protected Point startPoint;
        protected Point endPoint;
        protected LineCap startCap = LineCap.Round;
        protected LineCap endCap = LineCap.Round;
        
        private LineController controller;

        #endregion

        #region Methods

        public LineDevice(int x1, int y1, int x2, int y2): this(new Point(x1, y1), new Point(x2, y2)) 
        {}

        public LineDevice(Point start, Point end)
        {
            startPoint = start;
            endPoint = end;
            borderWidth = 4;
            borderColor = Color.DarkGray;
        }

        internal override void Draw(System.Drawing.Graphics g)
        {
            HatchBrush b = new HatchBrush(HatchStyle.Trellis, Color.DarkGray);
            Pen p = new Pen(b, BorderWidth);
            p.StartCap = startCap;
            p.EndCap = endCap;
            g.DrawLine(p, startPoint, endPoint);
        }

        internal void CalcLine()
        {
            //Find Location and Size
            if (startPoint.X < endPoint.X)
            {
                location.X = startPoint.X;
                size.Width = endPoint.X - startPoint.X;
            }
            else
            {
                location.X = endPoint.X;
                size.Width = startPoint.X - endPoint.X;
            }

            if (startPoint.Y < endPoint.Y)
            {
                location.Y = startPoint.Y;
                size.Height = endPoint.Y - startPoint.Y;
            }
            else
            {
                location.Y = endPoint.Y;
                size.Height = startPoint.Y - endPoint.Y;
            }
        }

        #endregion

        #region Properties

        public virtual Point StartPoint
        {
            get
            {
                return startPoint;
            }
            set
            {
                startPoint = value;
                CalcLine();
                OnAppearanceChanged(new EventArgs());
            }
        }

        public virtual Point EndPoint
        {
            get
            {
                return endPoint;
            }
            set
            {
                endPoint = value;
                CalcLine();
                OnAppearanceChanged(new EventArgs());
            }
        }

        public virtual LineCap StartCap
        {
            get
            {
                return startCap;
            }
            set
            {
                startCap = value;
                OnAppearanceChanged(new EventArgs());
            }
        }

        public virtual LineCap EndCap
        {
            get
            {
                return endCap;
            }
            set
            {
                endCap = value;
                OnAppearanceChanged(new EventArgs());
            }
        }

        #endregion

        #region Events


        #endregion
        #region IControllable Members

        IController IControllable.GetController()
        {
            if (controller == null)
                controller = new LineController(this);
            return controller;
        }

        #endregion

    }
}
